package com.abso.mp3tunes.locker.core.data;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.dom4j.Element;

/**
 * A playlist is a sequence of Locker tracks.
 */
public class Playlist extends AbstractLockerData {

	/** The unique identifier. */
	private String id;

	/** The title. */
	private String title;

	/** The name of the playlist file. */
	private String fileName;

	/** The number of tracks included on the playlist. */
	private int fileCount;

	/** The date of the last modification. */
	private Date dateModified;

	/** The total size (in bytes) of the tracks included on the playlist. */
	private long size;

	/**
	 * Constructs a new playlist.
	 * 
	 * @param item
	 *            the &lt;item&gt; XML element.
	 */
	public Playlist(Element item) {
		this.id = LockerDataUtils.getString(item, "playlistId");
		this.title = LockerDataUtils.getString(item, "playlistTitle");
		this.fileName = LockerDataUtils.getString(item, "fileName");
		this.fileCount = LockerDataUtils.getInt(item, "fileCount", -1);
		this.dateModified = LockerDataUtils.getDate(item, "dateModified");
		this.size = LockerDataUtils.getLong(item, "size", -1);
	}

	/**
	 * Returns the unique identifier.
	 * 
	 * @return the unique identifier.
	 */
	public String getId() {
		return id;
	}

	/**
	 * Returns the title.
	 * 
	 * @return the title.
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Returns the name of the playlist file.
	 * 
	 * @return the name of the playlist file.
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * Returns the number of tracks included on the playlist.
	 * 
	 * @return the number of tracks.
	 */
	public int getFileCount() {
		return fileCount;
	}

	/**
	 * Returns the date of the last modification, or <code>null</code> if such
	 * date is unknown.
	 * 
	 * @return the date of the last modification.
	 */
	public Date getDateModified() {
		return dateModified;
	}

	/**
	 * Returns the total size (in bytes) of the tracks included on the playlist,
	 * or <code>-1</code> if the size is unknown.
	 * 
	 * @return the total size.
	 */
	public long getSize() {
		return size;
	}

	/**
	 * Returns <code>true</code> if the playlist is one of the predefined
	 * playlists: random tracks, newest tracks, or recently played tracks.
	 * 
	 * @return <code>true</code> if the playlist is not a used-defined
	 *         playlist.
	 */
	public boolean isReadOnly() {
		return "RANDOM_TRACKS".equals(id) || "NEWEST_TRACKS".equals(id)
				|| "RECENTLY_PLAYED".equals(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return getId().hashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (!(obj instanceof Playlist)) {
			return false;
		}
		return getId().equals(((Playlist) obj).getId());
	}

}
